{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "16883624",
   "metadata": {},
   "source": [
    "#### 写出导入pandas的语句，并且给pandas别名为pd。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "98d4b0d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87c9c934",
   "metadata": {},
   "source": [
    "#### 创建s1，包含元素 [92, 67, 70, 88, 76] 及对应索引 [\"小明\", \"小红\", \"小杰\", \"小丽\", \"小华\"]。创建s2，包含元素 [95, 85, 60, 79, 76] 及对应索引 [\"小明\", \"小杰\", \"小宇\", \"小娟\", \"小彤\"]。创建后打印s1和s2。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "645a30ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明    92\n",
      "小红    67\n",
      "小杰    70\n",
      "小丽    88\n",
      "小华    76\n",
      "dtype: int64\n",
      "小明    95\n",
      "小杰    85\n",
      "小宇    60\n",
      "小娟    79\n",
      "小彤    76\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "s1 = pd.Series([92, 67, 70, 88, 76], index=[\"小明\", \"小红\", \"小杰\", \"小丽\", \"小华\"])\n",
    "s2 = pd.Series([95, 85, 60, 79, 76], index=[\"小明\", \"小杰\", \"小宇\", \"小娟\", \"小彤\"])\n",
    "print(s1)\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b02a9ae6",
   "metadata": {},
   "source": [
    "#### 让s1和s2的相同索引元素之间进行相加，若无法找到对应索引的值则将缺失值用0代替。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c2168a42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "小丽     88.0\n",
       "小华     76.0\n",
       "小娟     79.0\n",
       "小宇     60.0\n",
       "小彤     76.0\n",
       "小明    187.0\n",
       "小杰    155.0\n",
       "小红     67.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.add(s2, fill_value=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d50cf68",
   "metadata": {},
   "source": [
    "#### 输出s1的统计学相关数据，包括最大值、最小值、平均值、标准差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e8a05e76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count     5.000000\n",
       "mean     78.600000\n",
       "std      10.990905\n",
       "min      67.000000\n",
       "25%      70.000000\n",
       "50%      76.000000\n",
       "75%      88.000000\n",
       "max      92.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "859a6918",
   "metadata": {},
   "source": [
    "#### 把s1更新为所有数字加5后的结果，并进行输出。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "75799fe6",
   "metadata": {},
   "outputs": [],
   "source": [
    "s1 = s1 + 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b91337ed",
   "metadata": {},
   "source": [
    "#### 根据以下规则，输出一个新的Series，包含s1里各个学生成绩所对应的等级。\n",
    "> - A：90分及以上\n",
    "> - B：80分及以上 - 90分\n",
    "> - C：70分及以上 - 80分\n",
    "> - D：60分及以上 - 70分\n",
    "> - 不及格：60分以下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "019cd2e9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'int' object has no attribute 'apply'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[29], line 13\u001b[0m\n\u001b[0;32m     10\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m     11\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m不及格\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m---> 13\u001b[0m \u001b[43ms1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mapply\u001b[49m(get_grade_from_score)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'int' object has no attribute 'apply'"
     ]
    }
   ],
   "source": [
    "def get_grade_from_score(score):\n",
    "    if score >= 90:\n",
    "        return \"A\"\n",
    "    elif score >= 80: \n",
    "        return \"B\" \n",
    "    elif score >= 70:\n",
    "        return \"C\"\n",
    "    elif score >= 60:\n",
    "        return \"D\"\n",
    "    else:\n",
    "        return \"不及格\"\n",
    "        \n",
    "s1.apply(get_grade_from_score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "557f0423-1c81-4baa-9163-183172c0e531",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
